package com.hjx.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.StringReader;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

public class FileSpiliter {

    @Test
    public void testStringReader() throws IOException {

        String s = " ";
        System.out.println(s.getBytes().length);

        StringReader reader = new StringReader(s);
        int i = 0;
        while((i = reader.read()) != -1) {
            System.out.println("Ascii : " + i + "-" + (char) i);
        }
    }

    public static long[][] splitRange(long length, int rangeCount) {

        long[][] ranges = {
            {0, 999999},
            {1000000, 1999999},
            {2000000, 2999999},
            {3000000, 3999999},
            {4000000, 4999999},
            {5000000, 5554696}
        };
        return ranges;

    }

    public static long[][] checkStartAndEnd(long[][] ranges, RandomAccessFile randomAccessFile) throws IOException {
        long[][] newRanges = new long[6][2];
        for (int k = 0; k < ranges.length ; k++) {
            long begin = ranges[k][0];
            long end = ranges[k][1];

            randomAccessFile.seek(begin);
            int i = -1;
            int j = 0;
            while(begin > 0 && (i = randomAccessFile.read()) != 32) {
                j++;
//                System.out.println("Ascii : " + i + "-" + (char) i);
            }
            begin += j;
            newRanges[k][0] = begin;

            randomAccessFile.seek(end);
            i = -1;
            j = 0;
            while((i = randomAccessFile.read()) != 32 && i != -1) {
                j++;
//                System.out.println("Ascii : " + i + "-" + (char) i);
            }
            end += j;
            newRanges[k][1] = end;

        }
        return newRanges;
    }

    @Test
    public void testRandomAccessFile() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(new File("D:\\ws\\testGradle\\src\\test\\resources\\test.txt"), "r");
        long length = randomAccessFile.length();
        System.out.println(length);

        long[][] ranges = splitRange(length, 6);

        long[][] newRanges = checkStartAndEnd(ranges, randomAccessFile);

        for (long[] range : newRanges){
//            System.out.println(range[0]);
//            System.out.println(range[1]);

            randomAccessFile.seek(range[0]);

            FileOutputStream fos = new FileOutputStream(new File("D:\\ws\\testGradle\\src\\test\\resources\\split\\" + range[0] + "-" + range[1] + ".txt"));

            byte[]  buff = new byte[1024];
            //用于保存实际读取的字节数
            int hasRead=0;
            //循环读取
            while((hasRead=randomAccessFile.read(buff))>0){

                fos.write(buff);
                //打印读取的内容,并将字节转为字符串输入
                System.out.println(new String(buff,0,hasRead));

            }
        }



//        int i = 0;
//        while((i = randomAccessFile.read()) != -1) {
//            System.out.println("Ascii : " + i + "-" + (char) i);
//        }

    }
}
